package com.printeverywhere.delegator;

import static com.printeverywhere.util.AdminConstants.CALLED_ADD_METHOD;
import static com.printeverywhere.util.AdminConstants.ERROR_IN_ADDING_THE_RECORD;
import static com.printeverywhere.util.AdminConstants.GET_LIST_WITH_SEARCH_CRITERIA;
import static com.printeverywhere.util.AdminConstants.GET_LIST_WITH_SEARCH_LIST_CRITERIA;
import static com.printeverywhere.util.AdminConstants.RECORD_ADDED;
import static com.printeverywhere.util.AdminConstants.SESSION_CLOSED;
import static com.printeverywhere.util.AttributeConstants.CLOSING_SESSION;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.printeverywhere.dto.CrossPromotion;
import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.dto.Items;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.HibernateUtil;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SearchListCriteria;

public class CrossPromotionServiceDelegator {

	private static Logger logger = Logger.getLogger(CrossPromotionServiceDelegator.class);

	/**
	 * Append the crossPromotion object as new row in crossPromotion
	 * table.
	 *
	 * @param crossPromotion
	 *            the crossPromotion
	 *
	 * @return the string
	 *
	 * @throws HibernateException
	 *             the hibernate exception
	 */
	public static String add(CrossPromotion crossPromotion)
			throws HibernateException {
		logger.info(CALLED_ADD_METHOD);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			session.save(crossPromotion);
			transaction.commit();
			logger.info(RECORD_ADDED);
			return RECORD_ADDED;
		} catch (HibernateException e) {
			logger.error(ERROR_IN_ADDING_THE_RECORD);
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}
	/**
	 * Append the crossPromotion object as new row in crossPromotion
	 * table.
	 *
	 * @param crossPromotion
	 *            the crossPromotion
	 *
	 * @return the string
	 *
	 * @throws HibernateException
	 *             the hibernate exception
	 */
	public static String delete(CrossPromotion crossPromotion)
			throws HibernateException {
		logger.info(AdminConstants.CALLED_DELETE_METHOD);
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try {
			session.delete(crossPromotion);
			transaction.commit();
			logger.info(AdminConstants.RECORD_DELETED);
			return AdminConstants.RECORD_DELETED;
		} catch (HibernateException e) {
			logger.error(AdminConstants.ERROR_IN_DELETING_THE_RECORD);
			transaction.rollback();
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.close();
		}
	}

	/**
	 * Gets the list of the CrossPromotion objects based on the keys and
	 * values specified. It returns the list iff the condition in given criteria
	 * satisfies. For Ex: if it requires list for only those record with given
	 * itemId, it looks for the given searching criteria and retrieves
	 * list accordingly.
	 *
	 * @param searchCriteria
	 *            the search criteria
	 *
	 * @return the list
	 *
	 * @throws HibernateException
	 *             the hibernate exception
	 */
	public static List<CrossPromotion> getList(SearchCriteria searchCriteria)
			throws HibernateException {
		logger.info(GET_LIST_WITH_SEARCH_CRITERIA);
		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(CrossPromotion.class);
		String orderBY = searchCriteria.getOrderBy();
		if (orderBY != null) {
			if (searchCriteria.isAsc()) {
				criteria.addOrder(Order.asc(orderBY));
			} else {
				criteria.addOrder(Order.desc(orderBY));
			}
		}
		Set<String> keys = searchCriteria.getAttributeNames();
		Iterator<String> it = keys.iterator();
		String key = null;
		Object value = null;
		while (it.hasNext()) {
			key = it.next();
			value = searchCriteria.getAttribute(key);
			criteria.add(Restrictions.eq(key, value));
		}
		Transaction transaction = session.beginTransaction();
		List<CrossPromotion> list = new ArrayList<CrossPromotion>();

		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			for (String keyexception : keys) {
				logger.error(keys);
			}
			throw e;
		} finally {
			logger.info(SESSION_CLOSED);
			session.flush();
			HibernateUtil.closeSession();
		}
		return list;
	}

	public static CrossPromotion getCrossPromotionById(int id) throws HibernateException {
		if(logger.isInfoEnabled()){
			logger.info("Getting list for Items by Id".intern());
		}
		Session session = HibernateUtil.getSession();
		try{
			session.beginTransaction();
			Criteria criteria = session.createCriteria(CrossPromotion.class);
			criteria.add(Restrictions.eq(AttributeConstants.ID , id));
			List<CrossPromotion> crossPromotionList = criteria.list();
			return ((crossPromotionList.size() > 0) ? crossPromotionList.get(0) : new CrossPromotion());
		}catch (HibernateException e) {
			logger.error("Got exception when getting list for CrossPromotion by Id".intern());
			throw e;
		} finally {
			if(logger.isInfoEnabled()){
				logger.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}
}
